package com.awangyh.jdk8.redis;

import redis.clients.jedis.Jedis;

import java.util.List;

/**
 * Created by admin on 2017/4/19.
 */
public class RedisDemo {

    public static void main(String[] args) {
        try {
            Jedis jedis = new Jedis("localhost");
            System.out.println("Connection to server sucessfully");

            // =============================================================
            //设置 redis 字符串数据
            //jedis.set("runoobkey", "Redis tutorial");
            // 获取存储的数据并输出
            //System.out.println("Stored string in redis:: " + jedis.get("runoobkey"));
            // ===============================================================

            // ================================================================
            //存储数据到列表中
            jedis.lpush("tutorial-list", "Redis");
            jedis.lpush("tutorial-list", "Mongodb");
            jedis.lpush("tutorial-list", "Mysql");
            // 获取存储的数据并输出
            List<String> list = jedis.lrange("tutorial-list", 0 ,6);
            for(int i=0; i<list.size(); i++) {
                System.out.println("Stored string in redis:: "+list.get(i));
            }

            for(int i=3; i<list.size(); i++) {
                jedis.rpop("tutorial-list");
                System.out.println("Rpop string in redis:: "+list.get(i));
            }


            list = jedis.lrange("tutorial-list", 0 ,6);
            list.forEach(System.out::println);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
